// pages/integral/integral.js
const {
  config
} = require('../../utils/config.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    pageIndex: 1, //页码
    pageSize: 4, //页面数据大小
    hotArr: [],
    indexImgs : [
      "https://hqzy-erp.oss-cn-hangzhou.aliyuncs.com/1666771968177405.jpg",
      "https://hqzy-erp.oss-cn-hangzhou.aliyuncs.com/1666771987806061.jpg",
      "https://hqzy-erp.oss-cn-hangzhou.aliyuncs.com/1666772058520806.jpg"
    ],
    shop:0,
    sts : 0
  },
  /**
   * 积分变更记录与学员积分
   */
  getStuIntegralChangeRecord() {
    var that = this
    wx.request({
      url: `${config}/erp/erpWeChat/erpwechatintegralrecord/wechatIntegral`,
      data: {
        stuId: app.globalData.userInfo.id,
        deptId: app.globalData.deptId,
        limit: that.data.pageSize,
        page: that.data.pageIndex,
      },
      success(resp) {
        that.setData({
          integral: resp.data.stuIntegral.integral,
          integralChangeRecord: resp.data.erpWechatIntegralChangeRecord,
          integralGoods: resp.data.integralGoods.list,
          point :  resp.data.stuIntegral.integral
        })
      }
    })
  },
  previewImage(e){
    var imageList = []
    imageList.push(e.currentTarget.dataset.image)
    wx.previewImage({
      urls: imageList,
      currentUrl: e.currentTarget.dataset.image
    })
  },
  exchangeGoods(e){
    var that = this
    var goods = e.currentTarget.dataset.goods
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '提示',
      content: '将要兑换'+goods.goodsName+',扣除'+goods.goodsIntegral+'积分',
      success(res){
        if(res.confirm){
          if(that.data.integral - goods.goodsIntegral < 0 ){
            wx.showModal({
              cancelColor: 'cancelColor',
              title: '提示',
              content: '积分不足',
            })
            return
          }
          if(goods.goodsStock - 1 <0){
            wx.showModal({
              cancelColor: 'cancelColor',
              title: '提示',
              content: '库存不足',
            })
            return
          }
          wx.request({
            url: `${config}/erp/erpWeChat/erpwechatintegralrecord/exchangeGoods`,
            data:{
              goodsId: goods.goodsId,
              goodsName: goods.goodsName,
              goodsIntegral: goods.goodsIntegral,
              goodsStock: goods.goodsStock,
              stuId: app.globalData.userInfo.id,
              stuIntegral: that.data.integral
            },
            success(resp){
              if(resp.data.flag == true){
                wx.showToast({
                  title: '兑换成功!',
                })
                setTimeout(function(){
                  wx.switchTab({
                    url: '/pages/my/my',
                  })
                },1500)
              }else{
                wx.showToast({
                  title: '兑换失败',
                  icon: 'error'
                })
              }
            }
          })
        }else{
        }
      }
    })
  },
  goExchangeRecord(){
    wx.navigateTo({
      url: '/pages/exchangerecord/exchangerecord',
    })
  },
  goShare(){
    let a = this
    wx.showModal({
      cancelColor: 'cancelColor',
      showCancel: false,
      title: '提示',
      content: '被分享人必须点击【我的】->【登录】才能成功分享赚取积分',
      success(res){
        if (res.confirm) {
          a.onShareAppMessage(res)
        }
      }
    })
  },
  /**
   * 分页加载
   */
  pageLimit() {
    //上拉加载分页数据
    var that = this
    //存在提交记录 上拉执行  不存在不执行
    if (that.data.integralGoods != "") {
      wx.showLoading({
        title: '玩命加载中',
        mask: true,
      })
      that.data.pageIndex++ //页码自增
      //发起请求
      wx.request({
        url: `${config}/erp/erpWeChat/erpwechatintegralrecord/wechatIntegral`,
        data: {
          page: that.data.pageIndex,
          limit: that.data.pageSize,
          stuId: app.globalData.userInfo.id,
          deptId: app.globalData.deptId,
        },
        success(resp) {
          if (resp.data.code === 0) {
            that.setData({
              //拼接记录数据
              integralGoods: that.data.integralGoods.concat(resp.data.integralGoods.list)
            })
          }
          // 隐藏加载框
          wx.hideLoading();
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shop:options.shop
    })
    this.getStuIntegralChangeRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getStuIntegralChangeRecord()
    this.toClassifyPage()

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStuIntegralChangeRecord()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },
  // 课程兑换
  toClassifyPage(e){
    if(e !== undefined){
      this.setData({
        sts:e.currentTarget.dataset.sts
      })
    }
    var that = this
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    //发起请求
    wx.request({
      url: `${config}/wechat/integralGoods/goodsType`,
      data: {
        goodsType:that.data.sts,
        deptId:app.globalData.deptId
      },
      success(resp) {
        that.setData({
          hotArr:resp.data.data,
        })
        // 隐藏加载框
        wx.hideLoading();
      }
    })
  },

  // 商品兑换
  toLimitedTimeOffer(){
    var that = this
    //发起请求
    wx.request({
      url: `${config}/wechat/integralGoods/goodsType`,
      data: {
        goodsType:1,
        deptId: app.globalData.deptId,
      },
      success(resp) {
        that.setData({
          hotArr:resp.data.data
        })
        // 隐藏加载框
        wx.hideLoading();
      }
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '酷叮猫少儿编程',
      path: '/pages/index/index?&stuId='+app.globalData.userInfo.id+'&stuTel='+app.globalData.userInfo.stuTel+'&stuDeptid='+app.globalData.deptId+'&share=true',
      imageUrl: 'https://hqzy-erp.oss-cn-hangzhou.aliyuncs.com/kdm-erp/wx/share/kdmHome.png'
    }
  },
  toDetail(e){
    let that = this
    wx.navigateTo({
      url: '/pages/integral/goodsDetail/goods-detail?id='+e.currentTarget.dataset.goodid+'&point='+that.data.integral
    })
  }
})